﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;

namespace Web.admin.CompetitionManage
{
    public partial class AddOuter : System.Web.UI.Page
    {
        private TeacherService teacherService = new TeacherService();
        private AcademyService academyService = new AcademyService();
        private CompetitionService competitionService = new CompetitionService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AcademyList.DataSource = academyService.getAllAcademies();
                AcademyList.DataTextField = "ACA_NAME";
                AcademyList.DataValueField = "ACA_ID";
                AcademyList.DataBind();

                TeacherList.DataSource = teacherService.GetAllTeachers();
                TeacherList.DataTextField = "TEA_NAME";
                TeacherList.DataValueField = "TEA_ID";
                TeacherList.DataBind();

                string operation = Request["OPERATION"];
                if (operation == null)
                {
                    return;
                }
                string compId = Request["id"];
                if (compId == null)
                    return;

                if (operation == "Modify")
                {
                    ModifyOuter(compId);
                }
                else if (operation == "Delete")
                {
                    DeleteOuter(compId);
                }
            }
        }

        protected void ModifyOuter(string compId)
        {
            BoCompetitions comp = competitionService.getCompetition(compId);

            TeacherList.DataSource = teacherService.GetTeachersByAcademy(comp.COM_ACADEMY);
            TeacherList.DataTextField = "TEA_NAME";
            TeacherList.DataValueField = "TEA_ID";
            TeacherList.DataBind();
            TeacherList.SelectedValue = comp.COM_MANAGER;

            txtCompName.Text = comp.COM_NAME;
            txtCompId.Value = comp.COM_ID;
            AcademyList.SelectedValue = comp.COM_ACADEMY;
            HostBox.Text = comp.COM_HOST;
            UndertakeBox.Text = comp.COM_UNDERTAKE;
            LevelList.SelectedValue = comp.COM_LEVEL;
            KindEditor.Text = comp.COM_CONTENTS;

            foreach (ListItem item in listForm.Items)
            {
                if (comp.COM_FORM.ToString() == item.Value)
                    item.Selected = true;
                else
                    item.Selected = false;
            }

            CapitalBox.Checked = comp.COM_CAPITAL == 0 ? false : true;
        }

        protected void DeleteOuter(string compId)
        {
            BoCompetitions Competitions = new BoCompetitions();
            Competitions.COM_ID = compId;
            competitionService.DeleteCompetition(Competitions);
            Response.Redirect("ListOuter.aspx");
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            BoCompetitions comp = new BoCompetitions();
            comp.COM_TYPE = 1;
            comp.COM_NAME = txtCompName.Text;
            comp.COM_ACADEMY = AcademyList.SelectedValue;
            comp.COM_MANAGER = TeacherList.SelectedValue;
            comp.COM_HOST = HostBox.Text;
            comp.COM_UNDERTAKE = UndertakeBox.Text;
            comp.COM_LEVEL = LevelList.SelectedValue;
            comp.COM_FORM = Int32.Parse(listForm.SelectedValue);
            comp.COM_CAPITAL = CapitalBox.Checked ? 1 : 0;
            comp.COM_CONTENTS = KindEditor.Text;
            if (txtCompId.Value == "")
                competitionService.CreateCompetition(comp);
            else
            {
                comp.COM_ID = txtCompId.Value;
                competitionService.UpdateCompetition(comp);
            }

            Response.Redirect("ListOuter.aspx");
        }

        protected void AcademyList_SelectedIndexChanged(object sender, EventArgs e)
        {
            TeacherList.DataSource = teacherService.GetTeachersByAcademy(AcademyList.SelectedValue);
            TeacherList.DataTextField = "TEA_NAME";
            TeacherList.DataValueField = "TEA_ID";
            TeacherList.DataBind();
        }
    }
}